//var citiesList = "changsha, changzhou, chengdu, chongqing, dalian, daqing, dongguan, foshan, fuzhou, " +
//    "guilin, guiyang, haerbin, hangzhou, hefei, huaian, huhehaote, huizhou, jiaxing, jinan, kunming, " +
//    "linyi, luoyang, nanchang, nanjing, nanning, nantong, ningbo, qingdao, quanzhou, shenyang, shijiazhuang, " +
//    "suzhou, taiyuan, tianjin, weifang, weihai, wenzhou, wuhan, wuxi, xiamen, xian, xianyang, xuzhou, " +
//    "yangzhou, yantai, zhengzhou, zhenjiang, zhongshan, zhuhai";
var citiesList = "guangzhou";

var cities = citiesList.split(",");

var path = require('path');
var Crawler = require('../../lib/crawler');
//var connection = 'mongodb://localhost:27017/66xiaoqu';
var connection = 'mongodb://192.168.1.123:27017/66xiaoqu';

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var async = require('async');
//var sleep = require('sleep');

async.eachSeries(cities, function (city) {

//cities.forEach(function (city) {
    var dbcollection = 'xq_' + city;
    var dbcollectionRent = 'xq_' + city + '_rent';

    function init(callback) {
        MongoClient.connect(connection, function (err, db) {
            console.log("Connected correctly to server");
            dbconn = db;
            callback(null, 'init');
        });
    }

    function getIds(callback) {
        findCommids(dbconn, function (a) {
                ids = a;
                callback(null, 'getIds');
            }
        );
    }

    var findCommids = function (db, callback) {
        var sCollection = db.collection(dbcollection);
        sCollection.find({}).toArray(
            function (err, docs) {
                callback(docs);
            });
    };
    var count = 0;

    var counter = function () {
        console.log(++count)
    };

    var insertDocs = function (totalUrl, commId, callback) {
        var collection = dbconn.collection(dbcollectionRent);
        //collection.remove();
        var request = require('request'), cheerio = require('cheerio');

        request(totalUrl, function (err, resp, body) {
            console.log(totalUrl);
            if (!err && resp.statusCode == 200) {
                if (body != null) {
                    var rentData = body.toString();
                    var rentIndex = rentData.search("rentNum");
                    var rentString = rentData.substring(rentIndex, rentIndex + 15);
                    var rent = parseInt(rentString.replace(/[^0-9\.]+/g, ''));
                    var saleIndex = rentData.search("saleNum");
                    var saleString = rentData.substring(saleIndex, saleIndex + 15);
                    var sale = parseInt(saleString.replace(/[^0-9\.]+/g, ''));

                    var rentRate = function (rent, sale) {
                        var rateVal;
                        if (rent == 0) {
                            rateVal = 0
                        } else {
                            if (sale == 0) {
                                rateVal = 80
                            } else {
                                rateVal = parseInt(rent / (rent + sale) * 100);
                                if (rateVal > 80) {
                                    rateVal = 80
                                }
                            }
                        }
                        return rateVal;
                    };

                    collection.insert(
                        [{
                            commId: commId,
                            city: city,
                            rent: rent,
                            sale: sale,
                            rentRate: rentRate(rent, sale)
                        }]
                        , function (err, result) {
                            assert.equal(err, null);
                            assert.equal(1, result.result.n);
                            assert.equal(1, result.ops.length);
                            console.log(city + ": Inserted %s into the document collection", commId);
                            callback(result);
                        });
                } else {
                    collection.insert(
                        [{
                            commId: commId,
                            city: city,
                            rent: null,
                            sale: null,
                            rentRate: null
                        }]
                        , function (err, result) {
                            assert.equal(err, null);
                            assert.equal(1, result.result.n);
                            assert.equal(1, result.ops.length);
                            console.log(city + ": Inserted %s into the document collection", commId);
                            //callback(result);
                        });
                }
            }
        });
    };

    var dbconn = null;
    var ids = [];

    function start(callback) {
        console.log("starting...");

        ids.forEach(function (item) {
                var commId = item.commId;
                var totalUrl = 'http://' + city + '.anjuke.com/ajax/communityext/?commid=' + commId + '&useflg=onlyForAjax';
                //if(count % 100 == 0)
                //    sleep.sleep(5);
                insertDocs(totalUrl, commId, counter)
            }
        )
        ;
    }

// Start our job
    async.series([
            init,
            getIds,
            start
        ]
        , function (err, results) {
            console.log(results);
        });

})
;